$(document).ready(function(){//访问后台地址，不修改
	 
	 getMapImages();
	
	$("#sceneSearEvent").click(function () {	
		getMapImages();
    });
	
	
	// 上传图片
//	$("body").delegate('#mapImageUpload', 'change', function(){
		$('#mapImageUpload').on('change',function(){
			return scenebuilding_FileUpload_onselect();
	});
});

function scenebuilding_FileUpload_onselect(){
	var _File = document.getElementById("mapImageUpload").files[0];
	if(!/image\/\w+/.test(_File.type)) {
		layer.msg('请上传图片文件', {icon: 1,time: 2000});
		return false;
	}
	var formData = new FormData();
	formData.append("file",_File);
	formData.append("enctype","multipart/form-data");
	$.ajax({
		type: 'POST',
		url: '/mapimages/uploadImg',
		data: formData,
		processData: false,
		contentType: false,
		success:function(data){
			var params={
				filePath: data.data.url,
				fileName :data.data.name,
				tenantId:tenantId,
				type:resourceType
			}
			$.ajax({
				type : "post",
				url : "/mapimages",
				data: JSON.stringify(params),
				dataType:"json",
				headers: {"Content-Type": "application/json;charset=utf-8"},
				success : function(data) {
					layer.msg('上传成功', {icon: 1,time: 1000});
//						getDataByParam("场景搭建");
					// 动态追加 新增的图片信息
					if (data.data) {
						var imgHtml = getScenebuildingHtml(data.data);
						$("#scenebuildingList ul").append(imgHtml);
						
						//注册鼠标经过事件 显示删除图层
						$("#scenebuildingList ul").find('.sceneDel').hide();
						$("#scenebuildingList ul").find('li').mouseenter(function(){
							if($(this).find(".sceneDel").css("display")=="none"){
								$(this).find(".sceneDel").fadeIn("fast");
							}
						})
						$("#scenebuildingList ul").find('li').mouseleave(function(){
							if($(this).find(".sceneDel").css("display")!="none"){
								$(this).find(".sceneDel").fadeOut("fast");
							}
						})
						
						//注册删除事件
						$("#scenebuildingList ul").find('.sceneDel').click(function(){

							var id = $(this).attr("data");
							layer.confirm('是否删除该信息？', {
						         btn: ['确定','取消'] //按钮
						    },function(){
								delscenebuildingByid(id);
						    }, function(){
						    	layer.msg('已取消', {icon: 1, time: 1000});
						    });
						})
						
					} else {
						
						layer.msg("上传失败！", {time: 1000});
					}
						
				},error: function(request) {
					layer.msg("上传失败！", {time: 1000});
				}
			})
			
		}
	});
}
	
/*场景的详情*/
function getScenebuildingHtml(obj){
	var html='';
	html+='<li id="'+obj.oid+'">';
	html+='	<div class="sceneImg">';
	html+='	  <img src="'+obj.imagePath+'" alt="" />';
	html+='   <div class="sceneDel" data="'+obj.oid+'"><span><i></i></span></div>';
	html+=' </div>';
	html+=' <div class="sceneTxt">';
	html+='	  <span>'+obj.imageName+'</span>';
	html+='	  <label class="custom-checkbox" id="'+obj.oid+'Radio"><input id="'+obj.oid+'RadioInput" class="check-zhengshe" style="display:none;" type="radio" name="sbradio" value="'+obj.oid+'"><i></i></label>';
	html+=' </div>';
	html+='</li>';
	
	return html;
}
	
function getMapImages(){
	$("#scenebuildingList ul").html("");
	var html = "";
	var name = $("#sceneFileName").val();	
	var params = {"imageName":name,"tenantId":tenantId,"type":resourceType};
    $.ajax({
		type : "get",
		url : "/mapimages?t="+new Date().getTime(),
		data: params,
		dataType:"json",
		headers: {"Content-Type": "application/json;charset=utf-8"},
		success : function(data) {
			if(data.data&&data.data.length>0){
				for(j = 0,len=data.data.length; j < len; j++) {
					var d = data.data[j];
					html+=getScenebuildingHtml(d);
				}
			}
			
			//加载HTML
			$("#scenebuildingList ul").append(html);
			//注册鼠标经过事件 显示删除图层
			$("#scenebuildingList ul").find('.sceneDel').hide();
			$("#scenebuildingList ul").find('li').mouseenter(function(){
				if($(this).find(".sceneDel").css("display")=="none"){
					$(this).find(".sceneDel").fadeIn("fast");
				}
			})
			$("#scenebuildingList ul").find('li').mouseleave(function(){
				if($(this).find(".sceneDel").css("display")!="none"){
					$(this).find(".sceneDel").fadeOut("fast");
				}
			})
			
			//注册删除事件
			$("#scenebuildingList ul").find('.sceneDel').click(function(){

				var id = $(this).attr("data");
				layer.confirm('是否删除该图片？', {
			         btn: ['确定','取消'] //按钮
			    },function(){
					delscenebuildingByid(id);
			    }, function(){
			    	layer.msg('已取消', {icon: 1, time: 1000});
			    });
			})
		},error: function(request) {
			layer.msg("获取地图图片数据失败", {time: 2000});
        }
	})
}
	
//通过ID删除场景
function delscenebuildingByid(id){	
	$.ajax({
		type : "DELETE",
		url : "/mapimages/"+id,
		dataType:"json",
		headers: {"Content-Type": "application/json;charset=utf-8"},
		success : function(data) {
			if(data.data&&data.data==1){
				layer.msg('删除成功', {icon: 1, time: 1000});
				getMapImages();
				removeImagesByid(id);
				$("#mapImageUpload").val("");
			}else{
				layer.msg('删除失败', {icon: 1, time: 1000});
			}
		},error: function(request) {
			layer.msg("删除失败", {time: 2000});
		}
	})
}

function showXzMap(){
	showDiTu();
	getMapImages();
}
function showWxMap(){
	showWeiXingTu();
	getMapImages();
}

//标点
function setImageActivate(){
	var id = $('#scenebuildingList input[name="sbradio"]:checked ').val();
	if(!id){
		layer.msg('请选择一个场景', {icon: 0});
		return;
	}
	
	var scr = $('#scenebuildingList input[name="sbradio"]:checked ').parents('li').find('.sceneImg img').attr('src');
	var imageId = $('#scenebuildingList input[name="sbradio"]:checked ').parents('li').attr("id");
	locationImageActivate(scr,imageId);
}

//标点保存
function saveImageActivate(){
	var id = $('#scenebuildingList input[name="sbradio"]:checked ').val();
	var ex = getImageExtent();
	if(!id){
		layer.msg('请选择一个场景', {icon: 0});
		return;
	}else if(ex.length == 0){
		layer.msg('未绘制标点', {icon: 0});
		return;
	}
	
	var params= ex;
	$.ajax({
		type : "put",
		url : "/mapimages",
		data: JSON.stringify(params),
		dataType:"json",
		headers: {"Content-Type": "application/json;charset=utf-8"},
		success : function(data) {	
			var addtomap=[];
			for(j = 0,len=data.data.length; j < len; j++) {
				var d = data.data[j];
				if(d.imagePos && d.imagePos!=""){
					addtomap.push({url:d.imagePath,extent:d.imagePos,id:d.oid,imageId:d.oid,rotation:d.rotation});
				}
			}
			addImagesToMap(addtomap);
			layer.msg("保存成功");
		},error: function(request) {
			layer.msg("保存失败", {time: 2000});
	    }
	})
} 
function getRootPath(){
	//获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
	var curWwwPath=window.document.location.href;
	//获取主机地址之后的目录，如： /uimcardprj/share/meun.jsp
	var pathName=window.document.location.pathname;
	var pos=curWwwPath.indexOf(pathName);
	//获取主机地址，如： http://localhost:8083
	var localhostPaht=curWwwPath.substring(0,pos);
	//获取带"/"的项目名，如：/uimcardprj
	var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
	return localhostPaht;
}

